from cocos import menu, director
import pygame
from events import fj_event_dispatcher
import config


class BaseMenu(menu.Menu):
    # 初始化音效
    pygame.mixer.init()
    select_sound = pygame.mixer.Sound('sounds/card.ogg')

    def __init__(self):
        '''
        基础菜单
        功能
            1. 提供自定义菜单
            2. “退出游戏”菜单项（点击退出程序）
        非功能
            1. 滑动上去有音效
            2. 滑动上去有抖动效果
        '''
        super(BaseMenu, self).__init__()

        # 设置菜单项属性（包括大小、颜色等）
        self.font_item['font_size'] = 40
        self.font_item_selected['font_size'] = self.font_item['font_size']
        self.font_item['color'] = (255, 255, 255, 255)
        self.font_item_selected['color'] = (192, 192, 192, 255)

        exit_menu_item = menu.ImageMenuItem('imgs/btn3.png', self.game_over)

        menu_items = self.menu_items()
        menu_items.append(exit_menu_item)
        self.create_menu(menu_items,
                         selected_effect=menu.shake(),
                         unselected_effect=menu.shake_back())

    def menu_items(self):
        # 定义一个接口，让子类实现
        # 返回一个菜单数组
        raise NotImplementedError

    def game_over(self):
        '''
        结束游戏
        '''
        director.director.terminate_app = True


class GameStartMenu(BaseMenu):
    # def __init__(self):
    #     super(GameStartMenu, self).__init__()

    def menu_items(self):
        game_start_menuitem = menu.ImageMenuItem('imgs/btn1.png', self.game_start)
        return [game_start_menuitem]

    def game_start(self):
        '''
        进入游戏开始页面
        '''
        # print('Game Starting...')
        # director.director.replace(GamePlayPage.GamePlayScene())
        fj_event_dispatcher.dispatch_event('on_game_start')


class GameOverMenu(BaseMenu):
    def menu_items(self):
        game_restart_menuitem = menu.ImageMenuItem('imgs/btn2.png', self.game_restart)
        return [game_restart_menuitem]

    def game_restart(self):
        '''
        重新开始
        1. 发送通知on_game_start消息
        :return:
        '''
        fj_event_dispatcher.dispatch_event('on_game_start')


class GamePauseStatusMenu(BaseMenu):

    def menu_items(self):
        game_resume_menuitem = menu.ImageMenuItem('imgs/btn4.png', self.game_resume)
        return [game_resume_menuitem]

    def game_resume(self):
        '''
        发出“恢复游戏”消息
        :return:
        '''
        fj_event_dispatcher.dispatch_event('on_game_resume')


class GamePauseMenu(menu.Menu):
    '''
    暂停按钮
    '''

    def __init__(self):
        super(GamePauseMenu, self).__init__()
        game_pause_menuitem = menu.ImageMenuItem('imgs/pause.png', self.game_pause)
        self.create_menu([game_pause_menuitem])
        menu.fixedPositionMenuLayout([(40, config.WIN_HEIGHT - 40)])(self)

    def game_pause(self):
        '''
        点下暂停按钮后
        1. 发送on_game_pasue消息
        2. 弹出game_pause_state_layer
        :return:
        '''
        # print('Pause menuitem is pressed')
        fj_event_dispatcher.dispatch_event('on_game_pause')
